<template>
  <div>
    <head_>
      <h1  style="width: 150px">领券中心</h1>
    </head_>
    <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
    >
      <van-card
          v-for="(item,index) in list"
          :price=item.amount
          :desc=item.note
          :title=item.name
          @click="getcp(item)"
          thumb="https://img2.baidu.com/it/u=2098882037,2084636609&fm=253&fmt=auto&app=138&f=JPEG?w=650&h=489"
      >
        <template #tags>
          <van-tag plain type="danger">优惠码:{{ item.code }}</van-tag>
        </template>
        <template #num>
          <van-tag plain type="warning">余:{{ item.count }}</van-tag>
          <van-tag plain type="success">总:{{ item.publishCount }}</van-tag>
        </template>
      </van-card>
    </van-list>
  </div>
</template>

<script>
import Vue from 'vue';
import {Dialog, Notify} from 'vant';
import head_ from "./header";

export default {
  name: "usercoupon",
  components: {
    head_,
    [Dialog.Component.name]: Dialog.Component,
    [Notify.Component.name]: Notify.Component,
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      msg: "321321",
    };
  },
  methods: {
    onLoad() {
      // 异步更新数据,获取当前用户可以领取的优惠券
      this.axios.get("/coupon/global/" + window.localStorage.getItem('ms_username')).then(resp => {
        this.list = resp.data.data
        this.finished = true
      })
    },
    getcp(item) {
      let that=this
      Dialog.confirm({
        title: '警告',
        message: '确定领取？',
      })
          .then(() => {
            console.log()
            let couponUserVo = {};
            couponUserVo.tbCoupon = item
            couponUserVo.tbUser = {
              id:localStorage.getItem("userId"),
              username:localStorage.getItem("ms_username")
            }
            this.$http.post("/coupon/addUserCoupon", couponUserVo).then(resp => {
              if (resp.data.code === 2000) {
                Notify({type: 'success', message: '领取成功'});
                this.onLoad();
              } else {
                Notify({type: 'warning', message: '领取失败'});
                this.onLoad();
              }
            })
            this.$http.get("/customer/tbIntegrationHistory/insertIntegrationHistory/"+localStorage.getItem("ms_username")+"/"+couponUserVo.tbCoupon).then(resp=>{
              console.log(resp.data.data)
            })
          })
          .catch(() => {
            // on cancel
          });
    }
  },
}
</script>

<style scoped>

</style>
